package com.huawei.rbac.service.impl;

import com.huawei.rbac.domain.Department;
import com.huawei.rbac.mapper.DepartmentMapper;
import com.huawei.rbac.query.QueryObject;
import com.huawei.rbac.result.PageResult;
import com.huawei.rbac.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class DepartmentServiceImpl implements IDepartmentService{

    @Autowired
    private DepartmentMapper mapper;

    public void saveOrUpdate(Department d) {
        if(d.getId() == null){
            mapper.insert(d);
        }else {
            mapper.updateByPrimaryKey(d);
        }
    }

    public void delete(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Transactional(readOnly = true)
    public Department get(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Transactional(readOnly = true)
    public List<Department> listAll() {
        return mapper.selectAll();
    }

    @Transactional(readOnly = true)
    public PageResult<Department> query(QueryObject qo) {
        //查总条数
        int totalcount = mapper.selectForCount(qo);

        //总条数不为0的时候查结果集
        if(totalcount == 0){
            return new PageResult<Department>(qo.getCurrentPage(),qo.getPageSize());
        }
        List<Department> list = mapper.selectForList(qo);
        return new PageResult<Department>(list,qo.getCurrentPage(),qo.getPageSize(),totalcount);
    }
}
